package com.study.manage;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.study.manage.dto.AjaxResponse;
import com.study.manage.dto.BaseProperties;
import com.study.manage.exception.ManageException;

@Aspect
@Component
public final class WebMonitor extends BaseProperties {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @SuppressWarnings("rawtypes")
    @Around(value = "execution(* com.study.manage.mvc..*(..))")
    public Object aroundLoggerMonitor(ProceedingJoinPoint point) throws Throwable {
        AjaxResponse result = new AjaxResponse(false);
        try {
            return point.proceed();
        } catch (ManageException e) {
            result.setMsg(e.getMessage());
            logger.info(e.getMessage());
        } catch (Exception e) {
            result.setMsg(e.getMessage()).setCode("999");
            logger.error("系统异常", e);
        }
        Signature signature = point.getSignature();
        Class returnType = ((MethodSignature) signature).getReturnType();
        if (returnType == AjaxResponse.class) {
            return result;
        } else if (returnType == String.class) {
            return ERROR_500;
        }
        return ERROR_500;
    }
}
